import { CoolController, BaseController } from '@cool-midway/core';
import { CmsArticleEntity } from '../../entity/article';
import { CmsCategoryEntity } from '../../entity/category';
import { CmsArticleService } from '../../service/article';

/**
 * cms模块-文章管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: CmsArticleEntity,
  service: CmsArticleService,
  // 向表插入当前登录用户ID
  insertParam: ctx => {
    return {
      // 获得当前登录的后台用户ID，需要请求头传Authorization参数
      user_id: ctx.admin.userId,
    };
  },

  pageQueryOp: {
    keyWordLikeFields: ['a.title', 'a.description'],
    fieldEq: ['a.title AS title', 'a.description AS description'],
    select: ['a.*', 'b.categoryname'],
    join: [
      {
        entity: CmsCategoryEntity,
        alias: 'b',
        condition: 'a.category_id = b.id',
        type: 'leftJoin',
      },
    ],
  },
  listQueryOp: {
    fieldEq: ['status', 'category_id'],
  },
})
export class CmsAdminArticleController extends BaseController {}